/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月25日
 */
package com.massyframework.beanskin.runtime.framework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.function.Consumer;

import com.massyframework.beanskin.assembly.beans.BeanExistsException;
import com.massyframework.beanskin.assembly.beans.BeanRegistry;
import com.massyframework.beanskin.service.ExportServiceDescriptor;
import com.massyframework.beanskin.service.ExportServiceDescriptorBuilder;

/**
 * 简单的Bean注册器
 * @author huangkh
 *
 */
public class SimpleBeanRegistry implements BeanRegistry {
	
	private Map<String, SimpleBean> beanMap;

	/**
	 * 
	 */
	public SimpleBeanRegistry() {
		this.beanMap = new HashMap<>();
	}

	@Override
	public void addBean(String beanName, Object bean) throws BeanExistsException {
		Objects.requireNonNull(beanName, "\"beanName\" cannot be null.");
		Objects.requireNonNull(bean, "\"bean\" cannot be null.");
		
		SimpleBean sb = new SimpleBean(bean);
		if (this.beanMap.putIfAbsent(beanName, sb) != null) {
			throw new BeanExistsException(beanName);
		}
	}

	@Override
	public boolean containsBean(String beanName) {
		Objects.requireNonNull(beanName, "\"beanName\" cannot be null.");
		return this.beanMap.containsKey(beanName);
	}

	public void defineExportService(String beanName, Object bean, Consumer<ExportServiceDescriptorBuilder> consumer)
			throws BeanExistsException {
		Objects.requireNonNull(beanName, "\"beanName\" cannot be null.");
		Objects.requireNonNull(bean, "\"bean\" cannot be null.");
		Objects.requireNonNull(consumer, "\"consumer\" cannot be null.");
		
		ServiceLoader<ExportServiceDescriptorBuilder> sl = 
				ServiceLoader.load(ExportServiceDescriptorBuilder.class, this.getClass().getClassLoader());
		Iterator<ExportServiceDescriptorBuilder> it = sl.iterator();
		ExportServiceDescriptorBuilder builder = it.hasNext() ? it.next() : null;
		if (builder == null) {
			throw new RuntimeException("cannot found ExportServiceDescriptorBuilder with spi.");
		}
		
		SimpleBean sb = new SimpleBean(bean, builder);
		if (this.beanMap.putIfAbsent(beanName, sb) != null) {
			throw new BeanExistsException(beanName);
		}
		consumer.accept(builder);
		builder.beanName(beanName);
	}

	@Override
	public Object getBean(String beanName) {
		Objects.requireNonNull(beanName, "\"beanName\" cannot be null.");
		
		SimpleBean sb = this.beanMap.get(beanName);
		return sb==null? null: sb.getBean();
	}

	@Override
	public List<String> getBeanNames() {
		return new ArrayList<String>(this.beanMap.keySet());
	}

	public ExportServiceDescriptor getExportServiceDescriptor(String beanName) {
		Objects.requireNonNull(beanName, "\"beanName\" cannot be null.");
		
		SimpleBean sb = this.beanMap.get(beanName);
		if (sb != null) {
			ExportServiceDescriptorBuilder builder = sb.getBuilder();
			return builder== null? null: builder.build();
		}
		return null;
	}
}
